import { getToken, setToken, removeToken } from '@/utils/auth'
import { login as loginApi, getUserinfo as getUserinfoAPI } from '@/api/user'
const state = {
  token: getToken(),
  userInfo: {}
}
const mutations = {
  updateToken(state, token) {
    state.token = token
    setToken(token)
  },
  delToken(state) {
    state.token = null
    removeToken()
  },
  updataUserinfo(state, paye) {
    state.userInfo = paye
  }

}
const actions = {
  // 在store仓库内调用接口，后续方便随时调用
  async login(context, data) {
    // 在这个函数里去调用登录接口
    console.log('调用了登录接口', data)
    const res = await loginApi(data)
    // console.log(res)
    // 调用此方法，存token
    context.commit('updateToken', res)
  },
  async getUserinfo({ commit }, data) {
    const res = await getUserinfoAPI(data)
    console.log(res)
    commit('updataUserinfo', res)
  },
  // 当token过期时，调用方法
  Logout({ commit }) {
    // 将数据清空
    commit('delToken')
    commit('updataUserinfo', {})
  }

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
